﻿using UnityEngine;
using UnityEngine.UI;
using UniRx;
using System;

namespace MGFramework {
    public class DebugConsole : MonoBehaviour
    {
        private static Text debugText;
        private static Text buttonText;
        private static RectTransform content;
        private static float contentHeight;
        private static float contentWidth;
        private static bool isHide = true;
        private static bool isChange = false;

        private Button hideButton;

        private static string logInfo = "";
        void Awake()
        {
            transform.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width / 2, Screen.height - 20);
            contentHeight = transform.position.y * 2 - 20;
            contentWidth = Screen.width / 2;
            transform.localPosition = new Vector2(Screen.width * 0.75f, 0);
            content = transform.Find("Viewport/Content").GetComponent<RectTransform>();
            debugText = transform.Find("Viewport/Content/Text").GetComponent<Text>();
            buttonText = transform.Find("HideButton/Text").GetComponent<Text>();

            hideButton = transform.Find("HideButton").GetComponent<Button>();
            hideButton.OnClickAsObservable().Subscribe(_ => HidePanel()).AddTo(this);
            Observable.EveryEndOfFrame().Subscribe(_=>{
                if (!isHide && isChange) {
                    debugText.text = logInfo;
                    if (debugText.preferredHeight > contentHeight)
                    {
                        content.offsetMax = new Vector2(content.offsetMax.x, debugText.preferredHeight - contentHeight);
                    }
                    isChange = false;
                }
            })
            .AddTo(this);
        }
        public static bool Log(string str)
        {
            if (debugText != null)
            {
                logInfo += "<color=green>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") +  ":" + str + "</color>" + "\n";
                Debug.Log(str);
                isChange = true;
                return true;
            }
            return false;
        }
        public static bool Error(string str)
        {
            if (debugText != null)
            {
                logInfo += "<color=red>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ":" + str + "</color>" + "\n";
                Debug.LogError(str);
                isChange = true;
                return true;
            }
            return false;
        }
        /// <summary>
        /// 隐藏DebugConsole
        /// </summary>
        public void HidePanel()
        {
            if (isHide)
            {
                transform.localPosition -= new Vector3(contentWidth, 0, 0);
                buttonText.text = "隐藏";
                isHide = false;
            }
            else
            {
                transform.localPosition += new Vector3(contentWidth, 0, 0);
                isHide = true;
                buttonText.text = "显示";
            }
        }
    }
}